<?php
/**
 * CakeCMS后台 - 菜单model
 * 
 * @author zzdboy
 * @version 1.0
 */
class Menu extends AppModel {
	public $name = 'Menu';
	public $useTable = 'menu';
	public $primaryKey = 'id';
	
	/**
	 * 根据用户ID获取菜单相关信息
	 *
	 * @param int $id        	
	 */
	public function GetMenuByID($id) {
		$MenuInfo = $this->find ( 'first', array (
				'conditions' => array (
						'id' => $id 
				) 
		) );
		return $MenuInfo;
	}
	
	/**
	 * 添加菜单
	 *
	 * @param array $data        	
	 */
	public function add($data) {
		if (! is_array ( $data )) {
			return false;
		}
		if ($this->save ( $data )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 编辑菜单
	 *
	 * @param array $data        	
	 */
	public function edit($data) {
		if (! is_array ( $data )) {
			return false;
		}
		
		if ($this->save ( $data )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 删除菜单
	 *
	 * @param int $id        	
	 */
	public function del($id) {
		// 删除下的的子栏目
		$this->delete_child ( $id );
		
		if ($this->delete ( $id )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 递归删除栏目
	 *
	 * @param int $id        	
	 */
	private function delete_child($id) {
		$id = intval ( $id );
		if (empty ( $id )) {
			return false;
		}
		
		$list = $this->find ( 'all', array (
				'conditions' => array (
						'parentid' => $id 
				) 
		) );
		if (is_array ( $list )) {
			foreach ( $list as $val ) {
				if (! empty ( $val ['Menu'] ['id'] )) {
					$this->delete_child ( $val ['Menu'] ['id'] );
					$this->delete ( $val ['Menu'] ['id'] );
				}
			}
			return true;
		}
	}
	
	/**
	 * 当前位置
	 *
	 * @param $mid int        	
	 */
	public function current_pos($mid) {
		$mid = intval ( $mid );
		$return = $this->GetMenuByID ( $mid );
		$str = '';
		if ($return ['Menu'] ['parentid']) {
			$str = self::current_pos ( $return ['Menu'] ['parentid'] );
		}
		return $str . $return ['Menu'] ['name'] . ' > ';
	}
	
	/**
	 * 按父ID查找菜单子项
	 *
	 * @param $parentid integer
	 *        	父菜单ID
	 * @param $with_self integer
	 *        	是否包括他自己
	 */
	public function admin_menu($parentid, $with_self = 0) {
		$parentid = intval ( $parentid );
		
		// 角色权限处理
		App::import ( 'Model', 'Admin' );
		$Admin = new Admin ();
		
		// 载入Session类
		App::import ( "Model", 'CakeSession' );
		// 初始化Session类
		$Session = new CakeSession ();
		
		$AdminInfo = $Admin->GetAdminByID ( $Session->read ( 'admin_uid' ) );
		if ($AdminInfo && $AdminInfo ['Admin'] ['roleid'] != 1) {
			// 用户所属角色
			App::import ( 'Model', 'AdminRolePriv' );
			$AdminRolePriv = new AdminRolePriv ();
			$AdminRolePrivInfo = $AdminRolePriv->GetAdminRolePrivByID ( $AdminInfo ['Admin'] ['roleid'] );
			$data = unserialize ( $AdminRolePrivInfo ['AdminRolePriv'] ['data'] );
		}
		
		// 如果是超级管理员角色,显示所有菜单
		if ($AdminInfo ['Admin'] ['roleid'] == 1) {
			$conditions = array (
					'parentid' => $parentid,
					'display' => 1 
			);
			$result = $this->find ( 'all', array (
					'conditions' => $conditions 
			) );
		} else {
			$conditions = array (
					'parentid' => $parentid,
					'display' => 1,
					'id' => $data 
			);
			$result = $this->find ( 'all', array (
					'conditions' => $conditions 
			) );
		}
		
		if ($with_self) {
			$conditions = array (
					'parentid' => $parentid 
			);
			$result2 [] = $this->find ( 'all', $conditions );
			$result = array_merge ( $result2, $result );
		}
		return $result;
	}
	
	/**
	 * 获取菜单深度
	 *
	 * @param int $id        	
	 * @param array $array        	
	 * @param int $i        	
	 */
	public function get_level($id, $array = array(), $i = 0) {
		foreach ( $array as $n => $value ) {
			if ($value ['Menu'] ['id'] == $id) {
				if ($value ['Menu'] ['parentid'] == '0')
					return $i;
				$i ++;
				return $this->get_level ( $value ['Menu'] ['parentid'], $array, $i );
			}
		}
	}
}
?>